/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.user.collection;

import static com.dataoke.shoppingguide.app773026.bean.Constants.PRODUCT_ID_KEY;

import android.content.Intent;

import com.dataoke.shoppingguide.app773026.activity.detail.DataokeProductDetailActivity;
import com.dataoke.shoppingguide.app773026.db.FavoriteBean;
import com.dataoke.shoppingguide.app773026.base.BaseRecyclerFragment;
import com.dataoke.shoppingguide.app773026.base.adapter.BaseRecyclerAdapter;
import com.dataoke.shoppingguide.app773026.activity.user.adapter.CollectionAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.dataoke.shoppingguide.app773026.activity.detail.ProductDetailActivity;

import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class UserCollectionFragment extends BaseRecyclerFragment<UserCollectionContract.Presenter, FavoriteBean> implements
        UserCollectionContract.View, BaseRecyclerAdapter.OnItemLongClickListener {

    public static UserCollectionFragment newInstance() {
        return new UserCollectionFragment();
    }

    @Override
    protected void initData() {
        if (mPresenter != null)
            mPresenter.getCache(mContext);
        super.initData();
        mAdapter.setOnItemLongClickListener(this);
    }

    @Override
    protected void onItemClick(FavoriteBean item, int position) {
        switch (item.getType()) {
            default:
                if(item.getType() == 1) {
                    Intent intent = new Intent(getContext(), DataokeProductDetailActivity.class);
                    intent.putExtra(PRODUCT_ID_KEY, item.getGoodsId());
                    startActivity(intent);
                } else {

                    Intent intent = new Intent(getContext(), ProductDetailActivity.class);
                    intent.putExtra(PRODUCT_ID_KEY, item.getGoodsId());
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void onLongClick(int position) {
        final FavoriteBean collection = mAdapter.getItem(position);
        if (collection == null)
            return;
        BasePopupView popupView = new XPopup.Builder(getContext())
                .isDestroyOnDismiss(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .asConfirm("删除收藏", "是否确认删除该内容吗？",
                        "取消", "确定",
                        () -> mPresenter.getFavReverse(collection, position), null, false);
        popupView.show();
    }

    @Override
    public void showGetFavSuccess(int position) {
        mAdapter.removeItem(position);
    }

    @Override
    public void onRefreshSuccess(List<FavoriteBean> data) {
        super.onRefreshSuccess(data);
    }

    @Override
    protected BaseRecyclerAdapter<FavoriteBean> getAdapter() {
        return new CollectionAdapter(mContext);
    }
}
